﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Borton.Beans;
using Borton.Data;

namespace Borton
{
    class GenericGrid : DataGridView
    {
        Bean bean;
        Bean[] data;
        Panel panel;

        public GenericGrid(Bean bean)
        {
            this.bean = bean;
            InitializeComponent();
            createControls();
            initData();
            
        }

        private void initData() {
            data = bean.getAll();
            fillData();
        }

        public GenericGrid(Bean bean, String[] ids)
        {
            this.bean = bean;
            InitializeComponent();
            createControls();
            //TODO: load by ids
        }

        private void createControls()
        {
            for (int i = 0; i < bean.getFields().Length; i++ )
            {
                this.Columns.Add(ControlManager.createColumn(bean.getFields()[i]));
            }
        }

        private void InitializeComponent()
        {
            this.AllowUserToAddRows = false;
            this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.ReadOnly = true;
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom);
            this.Location = new System.Drawing.Point(0, 30);
            this.BackgroundColor = System.Drawing.SystemColors.ButtonFace;

            ToolStrip toolbar = createToolbar();

            panel = new Panel();
            panel.Dock = DockStyle.Fill;
            panel.Controls.Add(toolbar);
            panel.Controls.Add(this);
        }

        public Panel getPanel()
        {
            return panel;
        }

        private ToolStrip createToolbar()
        {
            ToolStrip toolbar = new ToolStrip();
            toolbar.Name = "toolbar" + bean.getTableName();
            toolbar.Text = "Toolbar";

            ToolStripButton btn = ControlManager.createToolbarButton("Refresh");
            btn.Click += new EventHandler(this.refresh_Click);
            toolbar.Items.Add(btn);
            
            btn = ControlManager.createToolbarButton("Add");
            btn.Click += new EventHandler(this.add_Click);
            toolbar.Items.Add(btn);
            btn = ControlManager.createToolbarButton("Edit");
            btn.Click += new EventHandler(this.edit_Click);
            toolbar.Items.Add(btn);
            btn = ControlManager.createToolbarButton("Delete");
            btn.Click += new EventHandler(this.delete_Click);
            toolbar.Items.Add(btn);
            toolbar.Items.Add(new ToolStripSeparator());
            for (int i = 0; i < bean.getFields().Length; i++)
            {
                if (bean.getFields()[i].type == FieldType.IDENTIFIER)
                {
                    btn = ControlManager.createBeanStripButton(((Identifier)bean.getFields()[i]).getBean().getTableName(), ((Identifier)bean.getFields()[i]).getBean());
                    btn.Click += new EventHandler(this.relatedItem_Click);
                    toolbar.Items.Add(btn);
                }
            }


            return toolbar;
        }

        private void relatedItem_Click(object sender, EventArgs e)
        {
            Bean bean = ((BeanStripButton)sender).bean;
            Form form = new Form();
            form.Text = bean.getTableName();
            
            form.ClientSize = new System.Drawing.Size(350, 266);
            form.AutoSize = true;
            form.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
            form.Controls.Add(new GenericGrid((Bean)bean.Clone()).getPanel());
            form.ShowDialog();
        }

        private void add_Click(object sender, EventArgs e)
        {
            Bean b = (Bean)bean.Clone();
            GenericForm form = new GenericForm(b, false);
            form.FormClosed += new FormClosedEventHandler(this.form_Closed);
            form.ShowDialog();
        }

        private void form_Closed(object sender, EventArgs e)
        {
            initData();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            if (this.SelectedRows.Count == 1)
            {
                Bean b = getBean(this.SelectedRows[0]);
                GenericForm form = new GenericForm(b, false);
                form.FormClosed += new FormClosedEventHandler(this.form_Closed);
                form.ShowDialog();
            }
            else
            {
                makeASelection();
            }
        }

        private Bean getBean(DataGridViewRow row)
        {
            Bean b = (Bean)bean.Clone();
            for (int i = 0; i < b.getFields().Length; i++)
            {
                b.getFields()[i].setValue( row.Cells[i].Value.ToString() );
            }
            return b;
        }

        private void refresh_Click(object sender, EventArgs e)
        {
            initData();
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (this.SelectedRows.Count == 1)
            {
                Bean b = getBean(this.SelectedRows[0]);
                b.delete();
                initData();
            }
            else
            {
                makeASelection();
            }
        }

        private void fillData()
        {
            this.Rows.Clear();
            for (int i = 0; i < data.Length; i++)
            {
                Object[] values = new Object[data[i].getFields().Length];
                for (int j = 0; j < data[i].getFields().Length; j++)
                {
                    values[j] = data[i].getFields()[j].getValueForDisplay();
                }
                this.Rows.Add(values);
            }
        }

        private void makeASelection()
        {
            MessageBox.Show("Please select a row first");
        }
    }
}
